/**
 * 
 */
package org.o14x.alpha.ui.commands;

import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.PlatformUI;
import org.o14x.alpha.ui.controller.UIController;
import org.o14x.alpha.ui.views.folder.FolderView;
import org.o14x.alpha.ui.views.foldertree.FolderTreeView;
import org.o14x.alpha.util.Messages;

/**
 * Delete command.
 * 
 * @author Olivier DANGREAUX
 */
public class DeleteCommand extends AbstractHandler {
	private UIController uiController;

	private static MessageFormat[] messageFormats;

	static {
		messageFormats[0] = new MessageFormat(Messages.getString("DeleteCommand.confirmation_message1")); //$NON-NLS-1$
		messageFormats[0] = new MessageFormat(Messages.getString("DeleteCommand.confirmation_message2")); //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		IStructuredSelection selection = null;

		if (PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart() instanceof FolderView) {
			selection = (IStructuredSelection) uiController.getDomain().getCurrentExplorerTab().getFolderView().getTableViewer().getSelection();
		} else if (PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart() instanceof FolderTreeView) {
			selection = (IStructuredSelection) uiController.getDomain().getCurrentExplorerTab().getExplorerTreeViewer().getSelection();
		}

		if (selection != null && !selection.isEmpty()) {
			String message = ""; //$NON-NLS-1$
			if (selection.size() == 1) {
				File file = (File) selection.getFirstElement();
				message = messageFormats[0].format(new String[] { file.getName() });
			} else {
				message = messageFormats[1].format(new String[] { String.valueOf(selection.size()) });
			}

			boolean reallyDelete = MessageDialog.openConfirm(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), Messages.getString("DeleteCommand.dialog_title"), message); //$NON-NLS-1$
			if (reallyDelete) {
				File parentFile = null;

				for (File file : (List<File>) selection.toList()) {
					try {
						// parent directory
						parentFile = file.getParentFile();

						// delete file
						FileUtils.forceDelete(file);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}

				// if delete command xas called from the explorer tree
				if (PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart() instanceof FolderTreeView) {
					// navigate to parent folder
					uiController.getDomain().getCurrentExplorerTab().navigate(parentFile);
				}

				// update display
				uiController.getDomain().getCurrentExplorerTab().getFolderView().update();
				uiController.getUi().getFolderTreeView().update();
			}
		}

		// TODO Auto-generated method stub
		return null;

	}

	/**
	 * Returns the value of uiController.
	 *
	 * @return The value of uiController.
	 */
	public UIController getUiController() {
		return uiController;
	}

	/**
	 * Sets the value of uiController.
	 *
	 * @param uiController The value of uiController to set.
	 */
	public void setUiController(UIController uiController) {
		this.uiController = uiController;
	}
}
